package ynu.edu.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ynu.edu.entity.Food;
import ynu.edu.exception.ResourceNotFoundException;
import ynu.edu.repository.FoodRepository;
import ynu.edu.service.FoodService;

import java.util.List;

/**
 * 食品服务实现类
 */
@Service
public class FoodServiceImpl implements FoodService {
    
    @Autowired
    private FoodRepository foodRepository;
    
    @Override
    public List<Food> getFoodsByBusinessId(Integer businessId) {
        return foodRepository.findByBusinessId(businessId);
    }
    
    @Override
    public Food getFoodById(Integer foodId) {
        return foodRepository.findById(foodId)
                .orElseThrow(() -> new ResourceNotFoundException("未找到ID为 " + foodId + " 的食品"));
    }
    
    @Override
    public Food getFoodByBusinessIdAndFoodId(Integer businessId, Integer foodId) {
        Food food = foodRepository.findByBusinessIdAndFoodId(businessId, foodId);
        if (food == null) {
            throw new ResourceNotFoundException("未找到商家ID为 " + businessId + " 且食品ID为 " + foodId + " 的食品");
        }
        return food;
    }
    
    @Override
    public List<Food> searchFoodsByBusinessIdAndName(Integer businessId, String foodName) {
        return foodRepository.findByBusinessIdAndFoodNameContaining(businessId, foodName);
    }
} 